<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>转账服务 - 智慧银行系统</title>
    <style>
        /* 全局样式 */
        body {
            margin: 0;
            font-family: "Microsoft YaHei", "微软雅黑";
            background-color: #f4f6f9;
            color: #333;
        }

        /* 顶部导航栏 */
        .top-nav {
            width: 95%;
            height: 80px;
            background-color: #1E90FF;
            display: flex;
            align-items: center;
            justify-content: space-between;
            color: white;
            padding: 0 30px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        }

        .top-nav .logo {
            font-size: 25px;
            font-weight: bold;
            letter-spacing: 1px;
            margin-left: 20px;
        }

        .top-nav .user {
            font-size: 16px;
        }

        .top-nav .user a {
            color: white;
            text-decoration: none;
            margin-left: 15px;
        }

        .top-nav .user a:hover {
            text-decoration: underline;
        }

        /* 页面整体布局 */
        .container {
            display: flex;
            min-height: calc(100vh - 60px);
            height: 100%;
        }

        /* 左侧导航栏 */
        .sidebar {
            width: 200px;
            background-color: #2F4F9D;
            color: white;
            display: flex;
            flex-direction: column;
            padding-top: 30px;
            min-height: calc(100vh - 60px);
        }

        .sidebar a {
            padding: 15px 20px;
            text-decoration: none;
            color: white;
            font-size: 16px;
            display: block;
            text-align: center;
        }

        .sidebar a:hover,
        .sidebar a.active {
            background-color: #4169E1;
            border-left: 5px solid #FFD700;
        }

        /* 主体内容 */
        .main-content {
            flex: 1;
            padding: 20px 40px 40px 20px;
        }

        .main-content h1 {
            color: #1E90FF;
            font-size: 26px;
            margin-bottom: 20px;
            margin-top: 0;
            text-align: left;
        }

        /* 转账表单样式 */
        .transfer-form {
            background-color: white;
            border-radius: 10px;
            padding: 40px;
            width: 600px;
            margin: 0 auto;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }

        .form-group {
            margin-bottom: 25px;
        }

        .form-group label {
            display: inline-block;
            width: 120px;
            font-size: 16px;
            font-weight: bold;
            margin-right: 20px;
            text-align: right;
        }

        .form-group input {
            width: 300px;
            height: 40px;
            padding: 0 15px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 16px;
        }

        .form-group input:focus {
            outline: none;
            border-color: #1E90FF;
            box-shadow: 0 0 5px rgba(30, 144, 255, 0.3);
        }

        .required {
            color: red;
            margin-right: 5px;
        }

        .amount-tip {
            color: #666;
            font-size: 14px;
            margin-top: 5px;
            margin-left: 145px;
        }

        .submit-btn {
            text-align: center;
            margin-top: 40px;
        }

        .submit-btn input {
            background-color: #1E90FF;
            color: white;
            border: none;
            width: 150px;
            height: 45px;
            font-size: 18px;
            font-weight: bold;
            border-radius: 5px;
            cursor: pointer;
            transition: background-color 0.3s;
        }

        .submit-btn input:hover {
            background-color: #0066CC;
        }

        /* 余额信息 */
        .balance-info {
            background-color: #f0f8ff;
            padding: 15px 20px;
            border-radius: 5px;
            margin-bottom: 20px;
            text-align: center;
        }

        .balance-info p {
            font-size: 18px;
            margin: 10px 0;
        }

        .balance-amount {
            color: #1E90FF;
            font-size: 24px;
            font-weight: bold;
        }

        /* 常用转账金额 */
        .quick-amounts {
            margin-left: 145px;
            margin-top: 10px;
        }

        .quick-amounts button {
            background-color: #f0f0f0;
            border: 1px solid #ddd;
            padding: 8px 15px;
            margin-right: 10px;
            border-radius: 4px;
            cursor: pointer;
            font-size: 14px;
        }

        .quick-amounts button:hover {
            background-color: #e0e0e0;
        }

        /* 响应式布局 */
        @media (max-width: 768px) {
            .container {
                flex-direction: column;
            }

            .sidebar {
                width: 100%;
                flex-direction: row;
                justify-content: space-around;
                padding: 10px 0;
            }

            .sidebar a {
                padding: 10px;
                font-size: 14px;
            }

            .transfer-form {
                width: 100%;
                padding: 20px;
            }

            .form-group label {
                display: block;
                width: 100%;
                text-align: left;
                margin-bottom: 10px;
            }

            .form-group input {
                width: 100%;
                box-sizing: border-box;
            }

            .amount-tip, .quick-amounts {
                margin-left: 0;
            }
        }
    </style>
</head>

<body>

    <!-- 顶部导航栏 -->
    <div class="top-nav">
        <div class="logo">智慧银行系统</div>
        <div class="user" id="user-info">
            <a href="login.html">登录&nbsp&nbsp</a> |
            <a href="register.html">注册</a>
        </div>
    </div>

    <div class="container">
        <div class="sidebar">
            <a href="index.html">首页</a>
            <a href="deposit.html">存款服务</a>
            <a href="withdraw.html">取款服务</a>
            <a href="transactions.html">账户流水</a>
            <a href="transfer.html" class="active">转账服务</a>
            <a href="user.html">个人中心</a>
        </div>

        <div class="main-content">
            <h1>转账服务</h1>
            
            <div class="transfer-form">
                <!-- 账户余额信息 -->
                <div class="balance-info">
                    <p>当前账户余额</p>
                    <p class="balance-amount">¥ <span id="current-balance">0.00</span></p>
                </div>

                <form id="transfer-form" onsubmit="return handleTransfer(event)">
                    <!-- 付款账户 -->
                    <div class="form-group">
                        <label for="source-card"><span class="required">*</span>付款账户</label>
                        <input type="text" id="source-card" placeholder="请输入付款卡号" maxlength="19" required>
                    </div>

                    <!-- 收款账户 -->
                    <div class="form-group">
                        <label for="target-card"><span class="required">*</span>收款账户</label>
                        <input type="text" id="target-card" placeholder="请输入收款卡号" maxlength="19" required>
                    </div>

                    <!-- 收款人姓名 -->
                    <div class="form-group">
                        <label for="recipient-name"><span class="required">*</span>收款人姓名</label>
                        <input type="text" id="recipient-name" placeholder="请输入收款人姓名" required>
                    </div>

                    <!-- 转账金额 -->
                    <div class="form-group">
                        <label for="transfer-amount"><span class="required">*</span>转账金额</label>
                        <input type="number" id="transfer-amount" placeholder="请输入转账金额" min="0.01" step="0.01" required>
                        <div class="amount-tip">请输入0.01元以上的金额</div>
                        <div class="quick-amounts">
                            <button type="button" onclick="setQuickAmount(100)">100元</button>
                            <button type="button" onclick="setQuickAmount(500)">500元</button>
                            <button type="button" onclick="setQuickAmount(1000)">1000元</button>
                            <button type="button" onclick="setQuickAmount(5000)">5000元</button>
                        </div>
                    </div>

                    <!-- 转账备注 -->
                    <div class="form-group">
                        <label for="transfer-remark">转账备注</label>
                        <input type="text" id="transfer-remark" placeholder="请输入转账备注（选填）">
                    </div>

                    <!-- 交易密码 -->
                    <div class="form-group">
                        <label for="transaction-pwd"><span class="required">*</span>交易密码</label>
                        <input type="password" id="transaction-pwd" placeholder="请输入交易密码" required>
                    </div>

                    <!-- 提交按钮 -->
                    <div class="submit-btn">
                        <input type="submit" value="确认转账">
                    </div>
                </form>
            </div>
        </div>
    </div>

    <script>
        // 模拟登录状态，可替换为后端逻辑
        const isLoggedIn = false; // 改成 true 测试登录状态
        const username = "张三";
        const gender = " 先生";

        // 设置用户信息
        const userInfo = document.getElementById("user-info");
        if (isLoggedIn) {
            userInfo.innerHTML = `亲爱的 ${username}${gender}`;
            // 设置模拟余额
            document.getElementById("current-balance").textContent = "10000.00";
        } else {
            // 非登录状态下，提示用户登录
            document.getElementById("current-balance").textContent = "请先登录";
        }

        // 常用金额设置函数
        function setQuickAmount(amount) {
            document.getElementById("transfer-amount").value = amount;
        }

        // 格式化卡号（每4位添加空格）
        function formatCardNumber(input) {
            input.addEventListener("input", function(e) {
                let value = e.target.value.replace(/\s/g, '');
                if (/^\d*$/.test(value)) {
                    e.target.value = value.replace(/(\d{4})/g, '$1 ').trim();
                } else {
                    e.target.value = value.substring(0, value.length - 1).replace(/(\d{4})/g, '$1 ').trim();
                }
            });
        }

        // 为卡号输入框添加格式化功能
        formatCardNumber(document.getElementById("source-card"));
        formatCardNumber(document.getElementById("target-card"));

        // 转账处理函数
        function handleTransfer(event) {
            event.preventDefault();
            
            if (!isLoggedIn) {
                alert("请先登录后再进行转账操作");
                window.location.href = "login.html";
                return false;
            }

            const sourceCard = document.getElementById("source-card").value.replace(/\s/g, '');
            const targetCard = document.getElementById("target-card").value.replace(/\s/g, '');
            const recipientName = document.getElementById("recipient-name").value;
            const transferAmount = parseFloat(document.getElementById("transfer-amount").value);
            const transferRemark = document.getElementById("transfer-remark").value;
            const transactionPwd = document.getElementById("transaction-pwd").value;

            // 表单验证
            if (sourceCard.length !== 16 && sourceCard.length !== 19) {
                alert("请输入正确的付款卡号（16位或19位）");
                return false;
            }

            if (targetCard.length !== 16 && targetCard.length !== 19) {
                alert("请输入正确的收款卡号（16位或19位）");
                return false;
            }

            if (!recipientName.trim()) {
                alert("请输入收款人姓名");
                return false;
            }

            if (isNaN(transferAmount) || transferAmount <= 0) {
                alert("请输入有效的转账金额");
                return false;
            }

            // 检查余额是否充足
            const currentBalance = parseFloat(document.getElementById("current-balance").textContent);
            if (transferAmount > currentBalance) {
                alert("余额不足，请检查转账金额");
                return false;
            }

            if (transactionPwd.length !== 6 || !/^\d+$/.test(transactionPwd)) {
                alert("交易密码必须为6位数字");
                return false;
            }

            // 模拟转账成功
            const remarkText = transferRemark ? `\n备注：${transferRemark}` : '';
            alert(`转账成功！\n付款账号：${sourceCard}\n收款账号：${targetCard}\n收款人：${recipientName}\n金额：¥${transferAmount.toFixed(2)}${remarkText}`);
            
            // 更新模拟余额
            document.getElementById("current-balance").textContent = (currentBalance - transferAmount).toFixed(2);
            
            // 重置表单
            document.getElementById("transfer-form").reset();
            
            return false;
        }
    </script>

</body>

</html>